package com.emc.atmos.test;

import java.io.IOException;
import java.io.InputStream;

import com.emc.esu.api.EsuApi;
import com.emc.esu.api.EsuException;
import com.emc.esu.api.rest.EsuRestApi;

public class AtmosUploader {

	private EsuApi esuAPI = null;
	
	private String atmosHOST = null;
	private int atmosPORT = 0;
	private String atmosFULLTOKENID = null;
	private String atmosSECRETKEY = null;
	
	private String ObjectToUpload = null;
	
	public AtmosUploader(String local_atmosHOST, int local_atmosPORT, String local_atmosFULLTOKENID, String local_atmosSECRETKEY)
    {
        this.atmosHOST = local_atmosHOST;
        this.atmosPORT = local_atmosPORT;
        this.atmosFULLTOKENID = local_atmosFULLTOKENID;
        this.atmosSECRETKEY = local_atmosSECRETKEY;
        
		this.esuAPI = getEsuApi();
        
    }
	
	
	
	
	public String UploadObjectRetOID(byte [] tempObjStream, long length)
	{
		String objID = null;
		try{
			Object oId;
			//oId = this.esuAPI.createObjectFromStream(null, null, tempObjStream, length, null);
			oId = this.esuAPI.createObject(null, null, tempObjStream, null);

			//tempObjStream.close();
			objID = oId.toString();
		}
		catch(EsuException ex){
				//log.error("atmos exception: " + ex.getMessage());
				//log.error(ex.getStackTrace());
				return null;
		}/*catch (IOException ex) {
				//log.error("error uploading object to atmos...");
				//log.error(ex.getStackTrace());
		}*/
	
		return objID;
	}
	private EsuApi getEsuApi(){
		if (esuAPI == null) {
			try{
				//esuAPI = new EsuRestApi(HOST, PORT, FULLTOKENID, SECRETKEY);
				esuAPI = new EsuRestApi(atmosHOST, 
											atmosPORT, 
											atmosFULLTOKENID, 
											atmosSECRETKEY); 
			}
			catch(EsuException ex){
				//log.error(ex.getMessage());
				ex.getMessage();
				return null;
			}
		}
		return esuAPI;
	}
	
}
